package com.hbnu.jdbc;

import com.hbnu.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 需求：使用封装好的数据库工具类进行模糊查询
 *
 * @author kittychen
 * @since 2025-10-22 15:11
 */
public class JdbcDemo07 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtils.getConnection();

            String sql = "select * from tb_user where username like ?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, "%a%"); // 设置参数
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                System.out.println(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet, preparedStatement, connection);
        }
    }
}
